package com.produce.system.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.produce.common.constant.SessionAttr;
import com.produce.common.util.Md5Helper;
import com.produce.common.util.MenuUtils;
import com.produce.system.constans.LoginConstants;
import com.produce.system.entity.SysMenu;
import com.produce.system.entity.SysUser;
import com.produce.system.service.SysMenuService;
import com.produce.system.service.SysUserService;
/**
 * @author WenPeng
 */
@Controller
@RequestMapping(value = "login")
public class LoginController {
	private Logger logger = Logger.getLogger(LoginController.class);
	@Resource
	private SysUserService sysUserService;
	@Resource
	private SysMenuService sysMenuService;
	/**
	 * 跳转到登录的页面
	 */
	@RequestMapping(value = "showLoginPage")
	public ModelAndView showLoginPage(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		return new ModelAndView(LoginConstants.LOGIN, modelMap);
	}
	/**
	 * 点击登录按钮 验证用户
	 */
	@RequestMapping("/checkUser")
	@ResponseBody
	public String checkUser(SysUser user,HttpServletRequest request){
		JSONObject json = new JSONObject();
		int usercount = sysUserService.findCountByUsername(user);
		logger.info(usercount);
		if(usercount == 0 ){//没有这个用户
			json.put("result", "login_NoUserError");
		}else if (usercount > 1){ //多个用户名重复的
			json.put("result", "login_ManyUserError");
		}else{//用户名不重复的 
			//开始验证用户名密码
			String password = user.getLoginpassword();
			user.setLoginpassword(Md5Helper.md5Str(password));
			SysUser validatedUser = new SysUser();
			validatedUser = sysUserService.validatePassword(user);
			if(validatedUser != null){
				HttpSession session = request.getSession();
				session.setAttribute(SessionAttr.USERIDBYLOGIN.getValue(), validatedUser.getUserid());
				session.setAttribute(SessionAttr.LOGINNAME.getValue(), user);
				List<String> userMenus = sysUserService.findMenuIdByUserId(validatedUser.getUserid());
				//根目录的parent id设置为 000000
				List<SysMenu> menuList = sysMenuService.getByParentId("000000");
				session.setAttribute(SessionAttr.USER_MENUS.getValue(), MenuUtils.getMenu(menuList, userMenus));
				logger.info("login success");
				json.put("result", "login_success");
			}else{
				json.put("result", "login_passwordError");
			}
		}
		return json.toString();
	}
	/**
	 * 登录后显示主页面  带菜单
	 */
	@RequestMapping("desktop")
	public ModelAndView showDesktop(HttpServletRequest request, String theme,
			ModelMap modelMap) {
		String userMenu = (String)request.getSession().getAttribute(SessionAttr.USER_MENUS.getValue());
		request.setAttribute("menus", userMenu);
		return new ModelAndView(LoginConstants.DESKTOP, modelMap);
	}
	/**
	 * 显示主页面
	 */
	@RequestMapping("showHome")
	public ModelAndView showHome(HttpServletRequest request, String theme,
			ModelMap modelMap) {
		return new ModelAndView(LoginConstants.Home, modelMap);
	}
}
